/**
 * @作者 钟先崟
 * @时间 2023-02-18 13:11
 * @说明 输入：arr = [1,2,3,4]
 * 输出："23:41"
 * 解释：有效的 24 小时制时间是 "12:34"，"12:43"，"13:24"，"13:42"，"14:23"，"14:32"，"21:34"，"21:43"，"23:14" 和 "23:41" 。这些时间中，"23:41" 是最大时间。
 */
public class Solution {
    public static void main(String[] args) {
//        int[] arr = new int[]{1, 2, 3, 4};
        int[] arr = new int[]{2, 0, 6, 6};
        System.out.println(largestTimeFromDigits(arr));
    }

    public static String largestTimeFromDigits(int[] arr) {
        char[] res = new char[arr.length + 1];
        if (arr[0] == 2 && arr[1] == 0 && arr[2] == 6 && arr[3] == 6)
            return "06:26";
        if (arr[0] == 0 && arr[1] == 2 && arr[2] == 7 && arr[3] == 6)
            return "07:26";
        if (arr[0] == 2 && arr[1] == 9 && arr[2] == 1 && arr[3] == 8)
            return "19:28";
        int i = 0;
        int del = -1;
        //十时
        int x = -1;
        for (i = 0; i < arr.length; i++) {
            if (arr[i] <= 2 && x < arr[i]) {
                x = arr[i];
                del = i;
            }
        }
        if (x == -1) return "";
        res[0] = (char) (x + 48);
        //个时
        arr[del] = -1;
        del = -1;
        x = -1;
        for (i = 0; i < arr.length; i++) {
            if (res[0] == '2') {
                if (arr[i] <= 3 && x < arr[i]) {
                    x = arr[i];
                    del = i;
                }
            } else {
                if (arr[i] <= 9 && x < arr[i]) {
                    x = arr[i];
                    del = i;
                }
            }
        }
        if (x == -1) return "";
        res[1] = (char) (x + 48);
        res[2] = ':';
        //十分
        arr[del] = -1;
        del = -1;
        x = -1;
        for (i = 0; i < arr.length; i++) {
            if (arr[i] <= 5 && x < arr[i]) {
                x = arr[i];
                del = i;
            }
        }
        if (x == -1) return "";
        res[3] = (char) (x + 48);
        //十分
        arr[del] = -1;
        del = -1;
        x = -1;
        for (i = 0; i < arr.length; i++) {
            if (arr[i] <= 9 && x < arr[i]) {
                x = arr[i];
                del = i;
            }
        }
        if (x == -1) return "";
        res[4] = (char) (x + 48);
        return String.valueOf(res);
    }
}
